/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.biz.domain.Profession;
import me.zhengjie.modules.biz.domain.ProfessionDetail;
import me.zhengjie.modules.biz.domain.req.ZsbProfessionReq;
import me.zhengjie.modules.biz.domain.resp.ProfessionQryResp;
import me.zhengjie.modules.biz.domain.vo.ProfessionQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.Date;
import java.util.List;

import me.zhengjie.modules.biz.service.ProfessionDetailService;
import me.zhengjie.modules.biz.service.ProfessionService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
 * @author yiwei.he
 * @date 2024-03-11
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "专业：专业管理")
@RequestMapping("/api/profession")
@Slf4j
public class ProfessionController {

    private final ProfessionService professionService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check()")
    public void exportProfession(HttpServletResponse response, ProfessionQueryCriteria criteria) throws IOException {
        professionService.download(professionService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询专业")
    @ApiOperation("查询专业")
    public ResponseEntity<PageResult<Profession>> queryProfession(ProfessionQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(professionService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增专业")
    @ApiOperation("新增专业")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> createProfession(@Validated @RequestBody Profession resources){
        resources.setCreateTime(new Date());
        resources.setUpdateTime(new Date());
        professionService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改专业")
    @ApiOperation("修改专业")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> updateProfession(@Validated @RequestBody Profession resources){
        resources.setUpdateTime(new Date());
        professionService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除专业")
    @ApiOperation("删除专业")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> deleteProfession(@RequestBody List<Long> ids) {
        professionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/professionList")
    @Log("专业列表查询")
    @ApiOperation("专业查询")
    public ResponseEntity<Object> professionList(@RequestBody ProfessionQueryCriteria qry) throws BizException {
        log.info("api/profession/professionList 专业列表查询");
        ProfessionQryResp resp = professionService.professionQry(qry);
        return new ResponseEntity<>(resp, HttpStatus.OK);
    }

    @PostMapping(value = "/list")
    @Log("专业列表（根据type区分本科专科）")
    @ApiOperation("专业列表（根据type区分本科专科）")
    public ResponseEntity<PageResult<Profession>> getListByType(@RequestBody ProfessionQueryCriteria criteria){
        log.info("api/profession/list 专业列表（根据type区分本科专科）：{}",criteria);
        return new ResponseEntity<>(professionService.getListByType(criteria,criteria.getPage()),HttpStatus.OK);
    }

    @PostMapping("/getListByName")
    @Log("根据院校名查询相关专业")
    @ApiOperation("专升本智能荐校专用")
    public ResponseEntity<PageResult<Profession>> zsbList(@Validated @RequestBody ZsbProfessionReq req){
        log.info("api/profession/getListByName 专升本智能荐校专用：{}",req);
        return new ResponseEntity<>(professionService.zsbList(req,req.getPage()), HttpStatus.OK);
    }

    @PostMapping("/pList")
    @Log("首页搜索专业【/api/profession/pList】、院校【/api/universities】")
    @ApiOperation("首页搜索专业、院校")
    public ResponseEntity<PageResult<Profession>> zsbList(@RequestBody ProfessionQueryCriteria req){
        log.info("api/profession/pList 首页搜索专业：{}",req);
        return new ResponseEntity<>(professionService.queryAll(req,req.getPage()), HttpStatus.OK);
    }

}